﻿#include "CPdfRptInterface.h"
#include "CPdfRptDataPkg.h"
#include "CTaskExecInfo.h"
#include "CPoolTaskRobRpt.h"
#include "CPoolTaskSynRpt.h"
#include "CPoolTaskRecRpt.h"
#include "CPoolTaskTearRpt.h"
#include "CPoolTaskRestAiRpt.h"
#include "CPdfRptBizHy.h"
#include "CPdfRptBizNd.h"
#include "CLogOper.h"

CPdfRptInterface::CPdfRptInterface(int nSmallSceneType, QObject *parent)
	: QObject(parent)
{

	m_pThreadPool = new QThreadPool();//new 线程池
	m_pThreadPool->setMaxThreadCount(1);//最大线程数设置1,按顺序执行输出
	
	//new 业务类 (按照不同的现场)
	if (0 == nSmallSceneType) //南电机器人
	{
		pRptBase = new CPdfRptBizNd();
	}
	else if (2 == nSmallSceneType)//弘扬机器人
	{
		pRptBase = new CPdfRptBizHy();
	}
	else//其他的现场
	{
		pRptBase = new CPdfRptBizHy();//其他默认使用弘扬
	}

	//new数据组包类
	m_pPdfRptDataPkg = new CPdfRptDataPkg();

}

CPdfRptInterface::~CPdfRptInterface()
{
	if (m_pThreadPool != nullptr)
	{
		m_pThreadPool->waitForDone();//等待所有任务完成后再销毁

		SAFE_DELETE(m_pThreadPool);
	}
	
	SAFE_DELETE(m_pPdfRptDataPkg);
	SAFE_DELETE(pRptBase);
}

//导出所有报表(包括机器人报表、综合报表、纠偏报表、撕裂报表、其他AI检测报表)
void CPdfRptInterface::expAllPdfRpt(CTaskExecInfo *pTaskExecInfo, QString sRobName, QString sPdfFolderPath, QString sHttpPrefix, QString sClientCpyName)
{
	if (m_pPdfRptDataPkg == nullptr || m_pThreadPool == nullptr)
	{
		return;
	}

	QDateTime dtLastTaskEndTime;//上一次任务结束时间
	QDateTime dtNowTaskEndTime;//本次任务结束时间
	QString sBeltNameOri = "";//皮带名称(原始未处理)
	QString sBeltNameDeal = "";//皮带名称(取#左边的处理后的)
	QString sBaCamSceneGuid = "";//球机场景Guid
	m_pPdfRptDataPkg->getPkgSearchParam(pTaskExecInfo->m_sGuid, dtLastTaskEndTime, dtNowTaskEndTime, sBeltNameDeal, sBeltNameOri, sBaCamSceneGuid);//使用组包类-->获取组包查询参数(机器人任务结束，查找前后两次任务时间、皮带名称)
	{
		//日志
		SExecLog exLog;
		
		exLog.m_nAlarmLevel = enWorkInfo;
		exLog.m_sDescriptionl = A2T("查询参数:皮带名称[%1],任务上次结束[%2]本次结束[%3],").arg(sBeltNameDeal).arg(dtLastTaskEndTime.toString("yyyy-MM-dd hh:mm:ss")).arg(dtNowTaskEndTime.toString("yyyy-MM-dd hh:mm:ss"));
		exLog.m_sDevName = sBeltNameDeal;
		exLog.m_sFuncName = A2T("报表");
		CLogOper::instance().addMsg(exLog);
	}

	QString sErrMsg = "";
	CAllPdfRptDataInfo dataInfo;//数据合集
	m_pPdfRptDataPkg->pkgPubPdfRptData(dtLastTaskEndTime, dtNowTaskEndTime, sPdfFolderPath, sHttpPrefix, sRobName, sBeltNameOri, dataInfo, sErrMsg);//使用组包类-->组包公共数据
	m_pPdfRptDataPkg->pkgRobPdfRptData(pTaskExecInfo, sRobName, sPdfFolderPath, sHttpPrefix, sClientCpyName, dataInfo, sErrMsg);//使用组包类-->组包机器人报表数据

	m_pPdfRptDataPkg->pkgRectifyPdfRptData(dtLastTaskEndTime, dtNowTaskEndTime, sBeltNameDeal, sPdfFolderPath, sHttpPrefix, dataInfo, sErrMsg);//使用组包类-->组包纠偏数据
	m_pPdfRptDataPkg->pkgTearPdfRptData(dtLastTaskEndTime, dtNowTaskEndTime, sBeltNameDeal, sPdfFolderPath, sHttpPrefix, dataInfo, sErrMsg);//使用组包类-->组包撕裂数据
	m_pPdfRptDataPkg->pkgRestAiPdfRptData(dtLastTaskEndTime, dtNowTaskEndTime, sBeltNameDeal, sPdfFolderPath, sHttpPrefix, dataInfo, sErrMsg);//使用组包类-->组包其他AI检测数据
	m_pPdfRptDataPkg->pkgSesePdfRptData(dtLastTaskEndTime, dtNowTaskEndTime, sBeltNameDeal, sPdfFolderPath, sHttpPrefix, dataInfo, sErrMsg);//使用组包类-->组包声振温检测数据
	m_pPdfRptDataPkg->pkgBaCamPdfRptData(dtLastTaskEndTime, dtNowTaskEndTime, sBeltNameDeal, sBaCamSceneGuid, sPdfFolderPath, sHttpPrefix, dataInfo, sErrMsg);//使用组包类-->组包球机检测数据

	//线程池中处理耗时任务 PS:线程的创建和销毁扔进线程池不需要我们维护
	//导出机器人报表(扔进线程池)
	CPoolTaskRobRpt *pPoolTaskRobRpt = new CPoolTaskRobRpt();//创建任务类对象(内部执行完自动delete)
	pPoolTaskRobRpt->setData(dataInfo.robDataInfo, pRptBase, pTaskExecInfo->m_sGuid);
	m_pThreadPool->start(pPoolTaskRobRpt);

	//导出综合报表(扔进线程池)
	CPoolTaskSynRpt *pPoolTaskSynRpt = new CPoolTaskSynRpt();//创建任务类对象(内部执行完自动delete)
	pPoolTaskSynRpt->setData(dataInfo, pRptBase, pTaskExecInfo->m_sGuid);
	m_pThreadPool->start(pPoolTaskSynRpt);

	if (true == dataInfo.pubDataInfo.m_bSynIncRec)
	{
		//导出单独的纠偏报表(扔进线程池)
		CPoolTaskRecRpt *pPoolTaskRecRpt = new CPoolTaskRecRpt();//创建任务类对象(内部执行完自动delete)
		pPoolTaskRecRpt->setData(dataInfo.rectifyDataInfo, pRptBase, pTaskExecInfo->m_sGuid);
		m_pThreadPool->start(pPoolTaskRecRpt);
	}
	
	if (true == dataInfo.pubDataInfo.m_bSynIncTear)
	{
		//导出单独的撕裂报表(扔进线程池)
		CPoolTaskTearRpt *pPoolTaskTearRpt = new CPoolTaskTearRpt();//创建任务类对象(内部执行完自动delete)
		pPoolTaskTearRpt->setData(dataInfo.tearDataInfo, pRptBase, pTaskExecInfo->m_sGuid);
		m_pThreadPool->start(pPoolTaskTearRpt);
	}
	
	if (true == dataInfo.pubDataInfo.m_bSynIncRestAi)
	{
		//导出单独的其他AI检测报表(扔进线程池)
		CPoolTaskRestAiRpt *pPoolTaskRestAiRpt = new CPoolTaskRestAiRpt();//创建任务类对象(内部执行完自动delete)
		pPoolTaskRestAiRpt->setData(dataInfo.extraAiDataInfo, pRptBase, pTaskExecInfo->m_sGuid);
		m_pThreadPool->start(pPoolTaskRestAiRpt);
	}
	

}

//导出单独机器人报表
void CPdfRptInterface::expSingleRobPdfRpt(CTaskExecInfo *pTaskExecInfo, QString sRobName, QString sPdfFolderPath, QString sHttpPrefix, QString sClientCpyName)
{
	if (m_pPdfRptDataPkg == nullptr || m_pThreadPool == nullptr)
	{
		return;
	}

	QString sErrMsg = "";
	CAllPdfRptDataInfo dataInfo;//数据合集
	m_pPdfRptDataPkg->pkgRobPdfRptData(pTaskExecInfo, sRobName, sPdfFolderPath, sHttpPrefix, sClientCpyName, dataInfo, sErrMsg);//使用组包类-->组包机器人报表数据

	//线程池中处理耗时任务 PS:线程的创建和销毁扔进线程池不需要我们维护
	//导出机器人报表(扔进线程池)
	CPoolTaskRobRpt *pPoolTaskRobRpt = new CPoolTaskRobRpt();//创建任务类对象(内部执行完自动delete)
	pPoolTaskRobRpt->setData(dataInfo.robDataInfo, pRptBase, pTaskExecInfo->m_sGuid);
	m_pThreadPool->start(pPoolTaskRobRpt);

}

//查询导出单独机器人报表
void CPdfRptInterface::expQuerySigRobPdfRpt(QString sTaskExecGuid, QString sPdfFolderPath, QString sHttpPrefix, QString sClientCpyName)
{
	QString sRobName = "";//机器人名称

	//new 任务执行数据
	CTaskExecInfo *pTaskExecInfo = new CTaskExecInfo();
	m_pPdfRptDataPkg->getPkgTaskExecInfo(sTaskExecGuid, pTaskExecInfo, sRobName);//获取组包任务执行信息(数据库中查找数据)

	expSingleRobPdfRpt(pTaskExecInfo, sRobName, sPdfFolderPath, sHttpPrefix, sClientCpyName);//导出单独机器人报表

	//delete 任务执行数据
	pTaskExecInfo->deleteDeepCopyData();//释放深拷贝数据
	SAFE_DELETE(pTaskExecInfo);
}

